package com.learn.mvc.controller;

import com.learn.mvc.bean.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ScopeController {


    /**
     * 使用servletAPI向request域对象共享数据
     * @param request
     * @return
     */
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request) {
        request.setAttribute("testRequestScope", "hello,servletAPI");
        return "success";
    }

    /**
     * 使用ModelAndView向request域对象共享数据
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        /**
         * ModelAndView有Model和View的功能
         * Model 主要用于向请求域共享数据
         * View 主要用于设置视图，经过视图解析器解析，实现页面跳转
         * 所有request域对象中共享数据的方法，最终都会将数据封装到ModelAndView对象中
         */
        ModelAndView mav = new ModelAndView();
        //处理model模型数据，即向请求域request共享数据
        mav.addObject("testRequestScope", "hello,ModelAndView");
        //设置view视图名称
        mav.setViewName("success");
        return mav;
    }

    /**
     * 使用Model向request域对象共享数据
     * @param model
     * @return
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope", "hello,model");
        System.out.println(model.getClass().getName());
        return "success";
    }


    /**
     * 使用map向request域对象共享数据
     * @param map
     * @return
     */
    @RequestMapping("/testMap")
    public String testMap(Map<String, Object> map){
        map.put("testRequestScope", "hello,map");
        System.out.println(map.getClass().getName());
        return "success";
    }

    /**
     * 使用ModelMap向request域对象共享数据
     * @param modelMap
     * @return
     */
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("testRequestScope", "hello,ModelMap");
        System.out.println(modelMap.getClass().getName());
        return "success";
    }


    /**
     * 在session域对象中共享数据
     * @param session
     * @return
     */
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("testSessionScope", "hello,session");
        return "success";
    }


    /**
     * 使用@SessionAttribute获取session中存在的属性
     * @param testSessionScope
     * @return
     */
    @RequestMapping("/testSession_get")
    public String testSession_get(@SessionAttribute(value = "testSessionScope")String testSessionScope) {
        System.out.println("从session获取到testSessionScope的值为"+testSessionScope);
        return "success";
    }

    // http://localhost:8080/springmvc/book1?book=123
    @RequestMapping("/book1")
    public String book1(String book,HttpSession session) {
        session.setAttribute("book", book);
        return "success";
    }
    // http://localhost:8080/springmvc/book2
    @RequestMapping("/book2")
    public String book2(@SessionAttribute(value = "book")String book) {
        System.out.println("从session中获取的参数为book"+book);
        return "success";
    }
    // http://localhost:8080/springmvc/book3
    @RequestMapping("/book3")
    public String book3(HttpSession session) {
        System.out.println("从session中获取的参数为book:"+session.getAttribute("book"));
        return "success";
    }



    /**
     * 通过HttpSession向application域对象共享数据
     * http://localhost:8080/springmvc/testApplication
     * @param session
     * @return
     */
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        // ServletContext，也叫application应用上下文对象。 代表整个应用的范围，包括从服务开启到关闭的范围。
        // 只跟服务器有关系。在服务器开启时创建，在服务器关闭时销毁。从头到尾只创建一次。
        //  为什么ServlceContext域对象能在整个工程中共享数据？因为大家都用的是同一个域对象。
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplicationScope", "hello,application");
        return "success";
    }

    /**
     * 通过HttpSession向application域对象get数据
     * http://localhost:8080/springmvc/testApplication_get1
     * @param session
     * @return
     */
    @RequestMapping("/testApplication_get1")
    public String testApplication_get1(HttpSession session){
        ServletContext application = session.getServletContext();
        System.out.println("从application中获取对应testApplicationScope的值为："+application.getAttribute("testApplicationScope"));
        return "success";
    }

    /**
     * 通过HttpServletRequest向application域对象get数据
     * http://localhost:8080/springmvc/testApplication_get2
     * @param request
     * @return
     */
    @RequestMapping("/testApplication_get2")
    public String testApplication_get2(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        System.out.println("从application中获取对应testApplicationScope的值为："+application.getAttribute("testApplicationScope"));
        return "success";
    }

}
